//
//  MovieController.m
//  WXMovie
//
//  Created by HZApple on 16/4/9.
//  Copyright © 2016年 无限互联. All rights reserved.
//

#import "MovieController.h"
#import "CommentModel.h"
#import "CommentCell.h"
#import "HeaderView.h"

@interface MovieController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableArray *detailArray;

@property (nonatomic, strong) HeaderView *header;


@end

@implementation MovieController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _loadData];
    
    _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
    _header = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    
    _tableView.tableHeaderView = _header;
    _tableView.tableHeaderView.userInteractionEnabled = YES;
    
    [_tableView registerClass:[CommentCell class] forCellReuseIdentifier:@"123"];
    
    // Do any additional setup after loading the view.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:_header];
    NSLog(@"%@",NSStringFromCGPoint(point));
    
}


- (void)_loadData {
    _dataArray = [NSMutableArray array];
    _detailArray = [NSMutableArray array];
    NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"movie_comment" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = dic1[@"list"];
    for (NSDictionary *dic in array) {
        CommentModel *model = [[CommentModel alloc] init];
        model.imageString = dic[@"userImage"];
        model.nickName = dic[@"nickname"];
        model.rating = dic[@"rating"];
        model.content = dic[@"content"];
        model.flag = [NSNumber numberWithBool:NO];
        model.height = 58;
        [_dataArray addObject:model];
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    CommentModel *model = _dataArray[indexPath.row];
    return model.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _dataArray[indexPath.row];
    cell.tag = indexPath.row + 100;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CommentCell *cell = [tableView viewWithTag:indexPath.row + 100];
    CommentModel *model = _dataArray[indexPath.row];
    BOOL flag = [model.flag boolValue];
    if (flag) {
        cell.view.frame = CGRectMake(70, 4, kScreenWidth - 80, 50);
        cell.contentLabel.frame = CGRectMake(10, 25, kScreenWidth - 80, 25);
        model.height = 58;
    } else {
        cell.contentLabel.numberOfLines = 0;
        cell.contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
        UIFont *font = [UIFont fontWithName:@"Heiti SC" size:16];
        
        CGRect tmpRect = [cell.contentLabel.text boundingRectWithSize:CGSizeMake(kScreenWidth - 80, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName ,nil] context:nil];
        
        cell.contentLabel.frame = CGRectMake(10, 25, kScreenWidth - 80, tmpRect.size.height);
        cell.view.frame = CGRectMake(70, 4, kScreenWidth - 80, tmpRect.size.height + 30);
        model.height = tmpRect.size.height + 38;
    }
    flag = !flag;
    model.flag = [NSNumber numberWithBool:flag];
    [tableView reloadData];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
